<template>
  <div class="base-info-view">
    <el-form
      class="form-detail"
      label-position="right"
      label-width="120px"
    >
      <el-row
        :gutter="20"
        style="width:100%"
      >
        <el-col :span="8">
          <el-form-item
            label="流程编号："
            style="width: 100%;"
          >
            <span>{{ detailData.processNo }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="创建时间："
            style="width: 100%;"
          >
            {{ dateFormat('yyyy-mm-dd HH:MM', detailData.startTime) }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="创建人："
            style="width: 100%;"
          >
            <span>{{ detailData.initiator }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <item-title title="配送商配送服务信息" />
    <el-form
      v-if="detail"
      class="form-detail"
      label-position="right"
      label-width="120px"
      style="padding-left:20px;"
    >
      <el-row
        :gutter="20"
        style="width:100%"
      >
        <el-col :span="8">
          <el-form-item label="配送商名称：">
            <div>
              <el-link
                type="primary"
                :underline="false"
                @click="toDistributeDetail"
              >{{ detail?.New?.distributeName }}</el-link>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="配送产品信息：">
            <div>
              <my-tooltip
                :content="getProductName(detail?.New)"
                @click="toProductDetail"
              />
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="配送服务编号：">
            <div>{{ detail?.New?.distributeServeNo }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="配送地区：">
            <area-list-view :areas="detail?.New?.region" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="关联配送合同：">
            <div>
              <el-link
                v-if="detail?.New?.contractNumber"
                type="primary"
                :underline="false"
                @click="toContractDetail"
              >{{ detail?.New?.contractNumber }}</el-link>
              <span v-else>无</span>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="服务结束日期：">
            <div>{{ dateFormat('yyyy-mm-dd', detail?.New?.expireTime) }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="配送费：">
            <div class="distribute-price flow-status-color">
              <div class="new-distribute-price">
                <div v-if="detail?.Old?.computeMode==='固定价格'">
                  <span>{{ detail?.Old?.distributePrice }}元</span>
                  <span v-if="detail?.Old?.sellUnit">/{{ getLabel('sell_unit',detail?.Old?.sellUnit) }}</span>
                </div>
                <div v-if="detail?.Old?.computeMode==='终端价百分比'">
                  <span>{{ detail?.Old?.distributePrice }}%</span>
                  <span>终端价</span>
                </div>
              </div>
              <div class="split"> --> </div>
              <div class="new-distribute-price">
                <div v-if="detail?.New?.computeMode==='固定价格'">
                  <span>{{ detail?.New?.distributePrice }}元</span>
                  <span v-if="detail?.New?.sellUnit">/{{ getLabel('sell_unit',detail?.New?.sellUnit) }}</span>
                </div>
                <div v-if="detail?.New?.computeMode==='终端价百分比'">
                  <span>{{ detail?.New?.distributePrice }}%</span>
                  <span>终端价</span>
                </div>
              </div>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="摘要信息："
            style="width: 100%"
          >
            <div>{{ detail?.New?.distributeAbstract || '无' }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="变更原因："
            style="width: 100%"
          >
            <div>{{ detail?.New?.changeReason || '无' }}</div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row
      v-if="detail?.New?.terminals?.length"
      style="width:100%;margin-top:20px;"
    >
      <el-col :span="24">
        <item-title title="同时要更新的关联配送终端关系的配送费" />
      </el-col>
      <el-col :span="24">
        <el-table
          ref="multipleTable"
          :data="detail?.New?.terminals"
          border
          style="width: 100%;"
        >
          <el-table-column
            label="终端名称"
            header-align="center"
            :min-width="160"
            show-overflow-tooltip
          >
            <template #default="scope">
              <el-link
                type="primary"
                :underline="false"
                @click="toTerminalDetail(scope.row)"
              >{{ scope.row.terminalName }}</el-link>
            </template>
          </el-table-column>
          <el-table-column
            label="终端类型"
            header-align="center"
            align="center"
            :min-width="100"
            show-overflow-tooltip
          >
            <template #default="scope">{{ getLabel('app_mode',scope.row.terminalType) }}</template>
          </el-table-column>
          <el-table-column
            prop="terminalPrice"
            label="终端价"
            header-align="center"
            align="center"
            :min-width="120"
            show-overflow-tooltip
          />
          <el-table-column
            prop="agentName"
            label="渠道客户名称"
            header-align="center"
            :min-width="160"
            show-overflow-tooltip
          >
            <template #default="scope">
              <el-link
                type="primary"
                :underline="false"
                @click="toAgentDetail(scope.row)"
              >{{ scope.row.agentName }}</el-link>
            </template>
          </el-table-column>
          <el-table-column
            label="当前配送费"
            header-align="center"
            :min-width="120"
            show-overflow-tooltip
          >
            <template #default="scope">
              <div v-if="scope.row.computeMode==='固定价格'">
                <span>{{ scope.row.oldDistributePrice }}</span><span>元</span><span v-if="scope.row.sellUnit"> / {{ getLabel('sell_unit',scope.row.sellUnit) }}</span>
              </div>
              <div v-if="scope.row.computeMode==='终端价百分比'">
                <span>{{ scope.row.oldDistributePrice }}</span><span>%终端价</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="更新后配送费"
            header-align="center"
            :min-width="120"
            show-overflow-tooltip
          >
            <template #default>
              <div>
                <div v-if="detail?.New?.computeMode==='固定价格'">
                  <span>{{ detail?.New?.distributePrice }}元</span>
                  <span v-if="detail?.New?.sellUnit">/{{ getLabel( 'sell_unit',detail?.New?.sellUnit) }}</span>
                </div>
                <div v-if="detail?.New?.computeMode==='终端价百分比'">
                  <span>{{ detail?.New?.distributePrice }}%</span>
                  <span>终端价</span>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 产品详情 -->
    <detail-drawer
      ref="proDetailDrawer"
      custom-title
    >
      <template #title>
        <span>产品详情 {{ getProductName(detail?.New) }}</span>
      </template>
      <template #contents>
        <div class="drawer-content">
          <div class="history-list">
            <item-title title="基本信息" />
            <product-base-info
              v-if="productId"
              :id="productId"
            />
          </div>
          <div class="history-list">
            <item-title title="修改记录" />
            <operation-view
              :id="productId"
              mode="product"
            />
          </div>
        </div>
      </template>
    </detail-drawer>
    <!--合同详情-->
    <detail-drawer
      ref="contractDrawer"
      custom-title
      :size-width="1000"
    >
      <template #title>
        <span>合同 {{ detail?.New?.contractNumber }} </span>
      </template>
      <template
        v-if="contractId"
        #contents
      >
        <div class="drawer-content">
          <div class="history-list">
            <contract-detail
              ref="salesDetailDrawer"
              :contract-id="contractId"
            />
          </div>
        </div>
      </template>
    </detail-drawer>
    <!--配送商详情-->
    <detail-drawer
      ref="distributeDrawer"
      custom-title
      :size-width="1000"
    >
      <template #title>
        <span>配送商 {{ detail?.New?.distributeName }} </span>
      </template>
      <template
        v-if="distributeId"
        #contents
      >
        <div class="drawer-content">
          <div class="history-list">
            <distributor-info :id="distributeId" />
          </div>
        </div>
      </template>
    </detail-drawer>
    <!--终端详情-->
    <detail-drawer
      ref="terminalDrawer"
      custom-title
      :size-width="1000"
    >
      <template #title>
        <span>终端 {{ rowData?.terminalName }} </span>
      </template>
      <template
        v-if="terminalId"
        #contents
      >
        <div class="drawer-content">
          <div class="history-list">
            <terminal-info :id="terminalId" />
          </div>
        </div>
      </template>
    </detail-drawer>
    <!--渠道客户详情-->
    <detail-drawer
      ref="agentDrawer"
      custom-title
    >
      <template #title>
        <span>渠道客户 - {{ rowData?.agentName }}</span>
      </template>
      <template #contents>
        <div class="drawer-content">
          <div class="history-list">
            <agent-info :id="agentId" />
          </div>
        </div>
      </template>
    </detail-drawer>
  </div>
</template>

<script>
import { defineAsyncComponent, reactive, ref, toRefs, watch } from 'vue'

export default {
  name: 'DistributorServiceInfo',
  components: {
    productBaseInfo: defineAsyncComponent(() =>
      import(
        '@CRM/views/commonView/detailComponents/product/productBaseInfo.vue'
      )
    ),
    contractDetail: defineAsyncComponent(() =>
      import('@CRM/views/commonView/contract/contractDetail.vue')
    ),
    distributorInfo: defineAsyncComponent(() =>
      import(
        '@CRM/views/commonView/detailComponents/distributor/distributorInfo.vue'
      )
    ),
    terminalInfo: defineAsyncComponent(() =>
      import('@CRM/views/commonView/detailComponents/terminal/terminalInfo.vue')
    ),
    agentInfo: defineAsyncComponent(() =>
      import('@CRM/views/commonView/detailComponents/agent/agentInfo.vue')
    ),
  },
  props: {
    detailData: {
      type: Object,
      default: () => {},
    },
  },
  setup(props, context) {
    var proDetailDrawer = ref(null)
    var contractDrawer = ref(null)
    var distributeDrawer = ref(null)
    var terminalDrawer = ref(null)
    var agentDrawer = ref(null)
    var reactiveData = reactive({
      detail: {},
      detailData: props.detailData,
      productId: 0,
      contractId: 0,
      distributeId: 0,
      terminalId: 0,
      agentId: 0,
      rowData: null,
    })
    const toProductDetail = () => {
      reactiveData.productId = reactiveData.detail?.New.productId
      proDetailDrawer.value.open()
    }
    const toContractDetail = () => {
      reactiveData.contractId = reactiveData.detail?.New.contractId
      contractDrawer.value.open()
    }
    const toDistributeDetail = () => {
      reactiveData.distributeId = reactiveData.detail?.New.distributeId
      distributeDrawer.value.open()
    }
    const toTerminalDetail = (row) => {
      reactiveData.rowData = row
      reactiveData.terminalId = row.terminalId
      terminalDrawer.value.open()
    }
    const toAgentDetail = (row) => {
      reactiveData.rowData = row
      reactiveData.agentId = row.agentId
      agentDrawer.value.open()
    }
    watch(
      () => props.detailData,
      (newval, oldval) => {
        if (newval) {
          reactiveData.detailData = newval
          reactiveData.detail = newval.data
        }
      },
      { immediate: true, deep: true }
    )
    return {
      ...toRefs(props),
      ...toRefs(reactiveData),
      proDetailDrawer,
      contractDrawer,
      distributeDrawer,
      terminalDrawer,
      agentDrawer,
      toProductDetail,
      toContractDetail,
      toDistributeDetail,
      toTerminalDetail,
      toAgentDetail,
    }
  },
}
</script>

<style lang="scss" scoped>
.base-info-view {
  .form-detail {
    .distribute-price {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .split {
        margin: 0px 4px;
      }
    }
  }
}
</style>
